package com.zoke.dappstore.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zoke.dappstore.Conf;
import com.zoke.dappstore.R;
import com.zoke.dappstore.adapter.DappAdapter;
import com.zoke.dappstore.base.NebActivity;
import com.zoke.dappstore.model.Comment;
import com.zoke.dappstore.model.Dapp;
import com.zoke.dappstore.tools.Tools;
import com.zoke.dappstore.ui.DappDetailActivity;
import com.zoke.neb.activities.BaseNebActivity;
import com.zoke.neb.base.BaseFragment;
import com.zoke.neb.tool.UToast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wulijie on 2018/6/11.
 */
public class RankF extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {


    @ViewInject(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @ViewInject(R.id.recycler)
    RecyclerView recyclerView;

    private DappAdapter adapter;

    private List<Dapp> mList = new ArrayList<>();

    private int fromId = -1;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.f_main);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.baseColor));
        refreshLayout.setOnRefreshListener(this);
        adapter = new DappAdapter(mList);
        adapter.openLoadAnimation();
        adapter.setRank(true);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        refreshLayout.setRefreshing(true);
        getDappList();
    }

    private void getDappList() {
        NebActivity neb = (NebActivity) context;
        neb.get("getHotAppList", new String[]{}, Tools.getAddress(), new BaseNebActivity.SmartCallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(String response) {
                LogUtil.e("response =" + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject jj = jsonObject.getJSONObject("result");
                    String jsonArray = jj.optString("result");
                    List<Dapp> list = new Gson().fromJson(jsonArray, new TypeToken<List<Dapp>>() {
                    }.getType());
                    if (list != null && list.size() != 0) {
                        if (fromId == -1) {
                            mList.clear();
                        }
                        for (Dapp dapp : list) {
                            int score = 0;
                            if (dapp.comment != null && dapp.comment.size() != 0) {
                                for (Comment comment : dapp.comment) {
                                    int cs = comment.score;
                                    score = score + cs;
                                }
                                score = score / dapp.comment.size();
                            }
                            dapp.score = score;
                            mList.add(dapp);
                        }
                        adapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    onError(e.toString());
                }
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String error) {
                refreshLayout.setRefreshing(false);
                LogUtil.e(error);
                UToast.show(context, "网络不给力哦：" + error.toString());
            }
        });
    }

    @Override
    public void onRefresh() {
        getDappList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        DappDetailActivity.open(context, mList.get(position));
    }
}
